package com.Web.guardians;

import com.DAO.Impl.ScheduleDAOImpl;
import com.DAO.Impl.StudentDAOImpl;
import com.DAO.Impl.guardiansDAOImpl;
import com.entity.Schedule;
import com.entity.Student;
import com.entity.guardians;
import com.entity.stuAndMenu;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 20x
 * @date 2024 - 12 -12
 **/
@WebServlet("/GetGuardianDetail")
public class GetGuardianDetail extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        System.out.println("GetGuardianDetail");
        String guardianId = req.getParameter("guardianId");
        guardians guardians = new guardiansDAOImpl().selectGuardian(guardianId);
        List<Student> students = new guardiansDAOImpl().getStudents(guardianId);
        Map<String, Object> responseData = new HashMap<>();
        List<stuAndMenu> stuAndMenuList =  new StudentDAOImpl().getStudentAndMenuById(students.get(0).getStuId());
        List<Schedule> scheduleList =  new ScheduleDAOImpl().queryScheduleAll(students.get(0).getStuId());
        responseData.put("stuAndMenuList", stuAndMenuList);
        responseData.put("scheduleList", scheduleList);
        responseData.put("guardian", guardians);
        responseData.put("students", students);

        // 将对象转换为JSON字符串
        String jsonResponse = gson.toJson(responseData);
        out.write(jsonResponse);
        out.close();
    }
}
